/**
 * 
 */
package org.istt.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.istt.dao.SubCategoryDao;
import org.istt.domain.SubCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

/**
 * @author Thond
 * 
 */
@Repository("subCategoryDao")
public class SubCategoryDaoHibernate extends BaseDaoHibernate<SubCategory, Long> implements SubCategoryDao {
	@Autowired
	public SubCategoryDaoHibernate(SessionFactory sessionFactory) {
		super(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SubCategory> findByCategoryId(final Long categoryId) {
		
		return (List<SubCategory>) hibernateTemplate
				.execute(new HibernateCallback<Object>() {

					@Override
					public Object doInHibernate(Session session) throws HibernateException, SQLException {
						
						Criteria crit = session.createCriteria(getPersistentClass());
						
						crit.add(Restrictions.eq("category.id", categoryId));
						
						return crit.list();
					}
				});
	}
	
}
